package com.ray.udp_send;

import java.awt.Component; 
import java.awt.Frame; 
import java.awt.Image;
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.media.Buffer;
import javax.media.CannotRealizeException; 
import javax.media.Manager; 
import javax.media.NoPlayerException; 
import javax.media.Player; 
import javax.media.Time;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JFrame;
 
public class myAVI implements Runnable{ 
	 public void run() {
	   	URL url = null; 
        try { 
            url = new URL("file:images/intro.avi"); 
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        }
        Player player = null;
        try { 
       	 player = Manager.createRealizedPlayer(url); 
        } catch (NoPlayerException e1) { 
            e1.printStackTrace(); 
        } catch (IOException e1) { 
            e1.printStackTrace(); 
        } catch (CannotRealizeException e) {
			e.printStackTrace();
		}  
	    AVIFrame sp = new AVIFrame(player);
	    sp.play();
	    while (true){
	    	sp.update_image();
	    }
	 } 
}

 class AVIFrame extends JFrame{
	private Frame f; 
    private Player player; 
    private Component visual;
    private FrameGrabbingControl fgc;
    private Buffer buffer=null;
    private BufferToImage bufferToImage=null;
    private Image image=null;
    
    public AVIFrame(Player player){
    	this.player=player;
    	this.setTitle("Send");
    	this.setSize(500,400);
    	this.setVisible(true);
    }
    public void play(){ 
        this.addWindowListener(new WindowAdapter() { 
            public void windowClosing(WindowEvent we) { 
                if(player != null) { 
                    player.close(); 
                } 
                System.exit(0); 
            } 
        }); 
        visual = player.getVisualComponent();
        this.add(visual);
        player.start();
        visual.getGraphics();
        fgc=(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
    }
    public void update_image(){
    	if   ( visual!=   null) {
      		buffer=fgc.grabFrame();
      		bufferToImage=new BufferToImage((VideoFormat)buffer.getFormat());
      		image=bufferToImage.createImage(buffer);
      		if (buffer.getLength()>0){
      			if (ReadImage.get_myResource().size()<300){
      				ReadImage.get_myResource().add(image);
//      				System.out.println(player.getState());
      				if (player.getState()==500) {
      					player.setMediaTime(new Time(0));
      					player.start();
      				}
      			}
      		}
      	}
    }
 }
